package com.project.base.baseproject.AppCustoms.SelectImageActivity;

import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.bm.library.PhotoView;
import com.jaeger.library.StatusBarUtil;
import com.project.base.baseproject.Activity.BaseActivity.BaseActivity;
import com.project.base.baseproject.R;
import com.squareup.picasso.Picasso;
import com.zxy.tiny.Tiny;
import com.zxy.tiny.callback.BitmapCallback;

import java.util.ArrayList;


//图片预览
public class ImageBrowseActivity extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_browse_layout);
        //intent中得到需要显示的图片
        ArrayList<BangBangPhoto> bangBangPhotos = new ArrayList<>();
        if(getIntent() != null)
            if(getIntent().getSerializableExtra("browseImage") != null)
                bangBangPhotos = (ArrayList<BangBangPhoto>) getIntent().getSerializableExtra("browseImage");
        int position = getIntent().getIntExtra("position",0);
        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
        final ArrayList<BangBangPhoto> arrayList = bangBangPhotos;
        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return arrayList.size();
            }
            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }
            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                final PhotoView photoView = new PhotoView(ImageBrowseActivity.this);
                photoView.enable();
                photoView.setMaxScale(4);
                final BangBangPhoto bangBangPhoto = arrayList.get(position);
                if(bangBangPhoto.getOiginalImage() != null) {
                    photoView.setImageBitmap(bangBangPhoto.getOiginalImage());
                } else {
                    Picasso.with(ImageBrowseActivity.this).load(bangBangPhoto.getOiginalUrl()).error(R.drawable.ic_launcher).placeholder(R.drawable.ic_launcher).into(photoView);
                }
                container.addView(photoView);
                return photoView;
            }
            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }
        });
        viewPager.setCurrentItem(position);
    }
}
